<?php
  include 'header.php';
  $qqq = 1;
  if ($_SESSION['user_level'] != 1) {header('location: signin.php');}
  $current_in = "";
  $uid = mysql_real_escape_string($_GET['u']);
  if (mysql_real_escape_string($_GET['u']) > 1) {
      $sql = $db->query("SELECT * FROM " . $table_prefix . "users WHERE user_id = $uid LIMIT 1");
      if (!$sql) {
          echo "An error occured! Please try again later.";
          die;
      }
      $row = $db->fetch_array($sql,'assoc');
      if (mysql_real_escape_string($_GET['u']) != $row['user_id']) {
          echo "An error occured! Please try again later.";
          die;
      }
      $pageTitle = $l_forumname . " | Member | " . $row['user_name'];
      $rank = $row['user_level'];
      if ($rank < 1) {
          $user_rank = "$l_rank_reg";
      }
      if ($rank == 1) {
          $user_rank = "<span style = 'color:red'>$l_administrator</span>";
      }
	  if ($rank == 2) {
          $user_rank = "<span style = 'color:green'>$l_mod</span>";
      }
      if ($rank < 0) {
          $user_rank = "<span style = 'color:#AAAAAA'></span>";
      }
      $user_ip = "";
      if ($_SESSION['user_level'] >= 1) {
          $user_ip = "<tr> <td height='32'><div align='right'>$l_user_ip</div></td><td><div align='left'>&nbsp;<span style = 'color:#536482'>$row[user_ip]</span></td></tr>";
      }
      
      $online = $db->query("SELECT user_id FROM " . $table_prefix . "online_users WHERE user_id = " . $row['user_id'] . " LIMIT 1");
      $qqq++;
      if ($user_status_posts == 1) {
          if (mysql_num_rows($online) > 0) {
              $u_online = "<img src='./style/$default_style/img/online.gif' title='online' align='absmiddle'/><br/>";
              //SHOW USER ACTIVIY ON PROFILE
              $current_in = '';
              if ($show_user_activity == 1) {
                  $track = $db->query("SELECT user_id,topic_id,mark_time FROM " . $table_prefix . "topics_track WHERE user_id = " . mysql_real_escape_string($_GET['u']) . " ORDER BY mark_time DESC LIMIT 1");
                  $qqq++;
                  if (mysql_num_rows($track) > 0) {
                      $topic_track = $db->fetch_array($track,'assoc');
                      
                      $track_topic_subject = $db->query("SELECT topic_subject FROM " . $table_prefix . "topics WHERE topic_id = " . $topic_track['topic_id'] . " LIMIT 1");
                      $topic_subject_is = $db->fetch_array($track_topic_subject,'assoc');
                      
                      $current_in = "<tr> <td height='32'><div align='right'>$l_current_view </div></td><td><div align='left'>&nbsp;<span style='color:#536482;'><a href='topic.php?t=" . $topic_track['topic_id'] . "'><b>" . $topic_subject_is['topic_subject'] . "</b></a></div></span></td></tr>";
                  }
              }
          } else {
              $u_online = "<img src='./style/" . $default_style . "/img/offline.gif' title='offline' align='absmiddle'/><br/>";
          }
      }
      
      $user_avatar = "<img width='32 px' height='32 px' src='../img/avatars/" . $row['user_avatar'] . "'>";
      if ($row['user_avatar'] == "") {
          $user_avatar = "No avatar";
      }
	  $user_is = $row['user_name'];
	  if ($row['is_banned'] == 1) {
	  $user_rank = "<span style = 'color:#AAAAAA'>Banned</span>";
	  $user_avatar = "";
	  $user_is = "<s>$row[user_name]</s>";
	  }
	  
      $tags = array('{VIEW}', '{USERNAME}', '{USER}', '{RANK}', '{USER_RANK}', '{JOINED}', '{LAST_VISIT}', '{USER_POSTS}', '{SEARCH_POSTS}', '{JOIN_DATE}', '{LOGIN_DATE}', '{SEARCH_USER_POSTS}', '{TOTAL_POSTS}', '{AVATAR}', '{AVATAR_PIC}', '{U_IP}', '{ONLINE}', '{USER_ACTIVITY}','{UID}','{F_URL}','{THIS_PAGE}');
      
      $last_login = date($date_format, strtotime($row['user_last_login']));
      if (strtotime($row['user_date']) > strtotime($row['user_last_login'])) {
          $last_login = "";
      }
      
      $data = array('<br/>' . $l_viewing_prof . '', $user_is, $l_un2, $l_group, $user_rank, $l_joined2, $l_last_visited, $l_total_posts, $l_search_u_posts, date($date_format, strtotime($row['user_date'])), $last_login, $uid, $row['user_posts'], $l_avatar, $user_avatar, $user_ip, $u_online, $current_in,$_GET['u'],$forum_url,'This page: ');
      
      echo str_replace($tags, $data, file_get_contents("./style/" . $default_style . "/user.html"));

  } else {
      echo "<br /><div align='center'>The requested user does not exist.</div>";
  }
  
  $pageContents = ob_get_contents();
  ob_end_clean();
  echo str_replace('<!--TITLE-->', $pageTitle, $pageContents);
?>